﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Text;
using System.Data;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 用品库存报警
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class OfficeItemInventoryAlarmController : AdminBaseController
    {
        //
        // GET: /Admin/OfficeItemInventoryAlarm/
        /// <summary>
        /// 用品库存报警主页
        /// </summary>
        /// <returns></returns>
        [AuthorityActionFilter("m_OfficeItemInventoryAlarm", Order = 1)]
        [LogPageActionFilter("用品库存报警", Order = 2)]
        public ActionResult Index()
        {
            StringBuilder strHtml = new StringBuilder();
            BLL.ItemInventoryAlarm inventoryAlarmBll = new BLL.ItemInventoryAlarm();
            DataSet ds = inventoryAlarmBll.GetAll();

            decimal count = 0;

            strHtml.Append("<table width='96%' border='1' class='tableForm'>");
            strHtml.Append("<tr height='25'>");
            strHtml.Append("<td align='center' class='tdClass' width='200'><font size='4px'>用品名称</font></td>");
            strHtml.Append("<td align='center' class='tdClass' width='100'><font size='4px'>规格</font></td>");
            strHtml.Append("<td align='center' class='tdClass' width='100'><font size='4px'>报警设置</font></td>");
            strHtml.Append("<td align='center' class='tdClass' width='100'><font size='4px'>当前库存</font></td>");
            strHtml.Append("<td align='center' class='tdClass'><font size='4px'>相关申请单</font></td></tr>");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                count++;
                strHtml.Append("<tr height='25'>");
                strHtml.Append("<td align='center' class='tdClass' width='200'><font size='3px'>" + dr["FItemName"].ToString() + "</font></td>");
                strHtml.Append("<td align='center' class='tdClass' width='100'><font size='3px'>" + dr["FItemSpec"].ToString() + "</font></td>");
                strHtml.Append("<td align='right' class='tdClass' width='100'><font size='3px'>" + dr["FSafetyStock"].ToString() + "</font></td>");
                strHtml.Append("<td align='right' class='tdClass' width='100'><font size='3px'>" + dr["FQty"].ToString() + "</font></td>");
                //循环申请单用,隔开

                DataSet ds1 = inventoryAlarmBll.GetByItemNo(dr["FItemNo"].ToString());
                strHtml.Append("<td align='left' class='tdClass'><font size='3px'>");
                decimal a = 0;


                if (ds1.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr1 in ds1.Tables[0].Rows)
                    {
                        a++;
                        if (a == ds1.Tables[0].Rows.Count)
                        {
                            strHtml.Append("<a href='#' onclick='popDialog(\"" + dr1["FItemRequisitionApplyID"].ToString() + "\")'>" + dr1["FItemRequisitionApplyID"].ToString() + "</a>");
                        }
                        else
                        {
                            strHtml.Append("<a href='#' onclick='popDialog(\"" + dr1["FItemRequisitionApplyID"].ToString() + "\")'>" + dr1["FItemRequisitionApplyID"].ToString() + "</a>,");
                        }
                    }
                }
                else 
                {
                    strHtml.Append("无");
                }

                strHtml.Append("</font></td></tr>");

                
            }
            if (count > 0)
            {
                strHtml.Append("<tr height='25'><td colspan='5' align='right' class='tdClass'><font size='4px'>以上" + count + "种办公用品出现库存告急！</font></td></tr>");
            }
            else
            {
                strHtml.Append("<tr height='25'><td colspan='5' align='right' class='tdClass'><font size='4px'>没有库存出现告急的办公用品</font></td></tr>");
            }
            strHtml.Append("</table>");

            ViewData["strHtml"] = strHtml;

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="itemRequisitionApplyID"></param>
        /// <returns></returns>
        public ActionResult OfficeItemInventoryAlarmInfo(string itemRequisitionApplyID)
        {
            Model.ItemRequisitionApply itemRequisitionApplyModel = new Model.ItemRequisitionApply();
            BLL.ItemRequisitionApply itemRequisitionApplyBll = new BLL.ItemRequisitionApply();
            itemRequisitionApplyModel = itemRequisitionApplyBll.GetModel(int.Parse(itemRequisitionApplyID));

            BLL.HRDept deptBll = new BLL.HRDept();
            BLL.HREmpl emplBll = new BLL.HREmpl();
            string entityNo = GlobalConstant.ENTITYNO;
            itemRequisitionApplyModel.FApplyDeptName = deptBll.GetDeptName(entityNo, itemRequisitionApplyModel.FApplyDeptNo);
            itemRequisitionApplyModel.FApplyEmplName = emplBll.GetEmpName(entityNo, itemRequisitionApplyModel.FApplyEmplNo);

            BLL.ItemRequisitionApplyEntry entryBll = new BLL.ItemRequisitionApplyEntry();
            List<Model.ItemRequisitionApplyEntry> entryModelList = entryBll.GetListEntry(decimal.Parse(itemRequisitionApplyID));

            BLL.Item itemBll = new BLL.Item();
            foreach (Model.ItemRequisitionApplyEntry model in entryModelList)
            {
                model.FItemName = itemBll.GetItemNo(model.FItemNo);
                
            }
            string json = "";
            if (entryModelList.Count > 0)
            {
                json = JsonConvert.SerializeObject(entryModelList);
            }
            ViewData["json"] = json;

            return View(itemRequisitionApplyModel);
        }


    }
}
